package com.fit.pages.registration;

import java.util.List;

import org.apache.tapestry5.annotations.AfterRender;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.beaneditor.BeanModel;
import org.apache.tapestry5.grid.GridDataSource;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.json.JSONObject;
import org.apache.tapestry5.services.AssetSource;
import org.apache.tapestry5.services.BeanModelSource;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;
import org.got5.tapestry5.jquery.internal.TableInformation;

import com.fit.dao.registration.LicencePlateDAO;
import com.fit.entities.LicencePlate;

public class BrowseRegistrations1 {

	@Property
	private List<LicencePlate> registrations;
	
	@Property
	private LicencePlate registration;
	
	@Inject
	private LicencePlateDAO registrationDAO;
	
	@Persist
	private String error;
	
	@Persist
	private String confirmation;
	
	void onActivate() {
		registrations = registrationDAO.findAll();
	}
	
	@OnEvent(component = "deleteLink")
	Object onDeleteLink(String licencePlateNo) {
		error="";
		confirmation="";
		LicencePlate r = registrationDAO.findByLicencePlateNo(licencePlateNo);
		try {
			registrationDAO.deleteByLicencePlateNo(licencePlateNo);
			confirmation = "Registration successfully deleted.";
			return null;
		} catch (Exception e) {
			error = "Error deleting registration. ";
			return null;
		}
	}
	
	@OnEvent(component = "ResetConfLink")
	Object onResetConfLink() {
		confirmation="";
		return null;
	}
	
	@OnEvent(component = "ResetErrorLink")
	Object onResetErrorLink() {
		error="";
		return null;
	}
	
	
	public boolean isErrorNotNull(){
		if(error==null || error==""){
			return false;
		}
		return true;
	}
	
	public boolean isConfirmationNotNull(){
		if(confirmation==null || confirmation==""){
			return false;
		}
		return true;
	}

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

	public String getConfirmation() {
		return confirmation;
	}

	public void setConfirmation(String confirmation) {
		this.confirmation = confirmation;
	}
	
	
}
